﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
using System.Net.Sockets;

namespace LiveUpdate
{
    public class SktClient
    {
        private string SktIP, SktPort;
        private IPEndPoint ServerInfo;
        private Socket ClientSocket;
        //信息接收缓存
        private Byte[] MsgBuffer;
        //信息发送存储
        private Byte[] MsgSend;

        public  SktClient()
        {
        }


        public string RequestDbInfo(string SktIP, string SktPort)
        {
            //定义一个IPV4，TCP模式的Socket
            ClientSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
            MsgBuffer = new Byte[100];
            MsgSend = new Byte[100];
            //允许子线程刷新数据
            //CheckForIllegalCrossThreadCalls = false;

            //服务端IP和端口信息设定,这里的IP可以是127.0.0.1，可以是本机局域网IP，也可以是本机网络IP
            ServerInfo = new IPEndPoint(IPAddress.Parse(SktIP), Convert.ToInt32(SktPort));

            try
            {
                //客户端连接服务端指定IP端口，Sockket
                ClientSocket.Connect(ServerInfo);
                //将用户登录信息发送至服务器，由此可以让其他客户端获知
                string sRequestStr = "DbPwd";
                int iLen = Encoding.GetEncoding("utf-8").GetBytes(sRequestStr).ToArray().Length;
                byte[] b0 = BitConverter.GetBytes(iLen);
                byte[] b1 = Encoding.GetEncoding("utf-8").GetBytes(sRequestStr);

                byte[] b2 = new byte[50];
                b0.CopyTo(b2, 0);
                b1.CopyTo(b2, b0.Length);

                ClientSocket.Send(b2);
                //开始从连接的Socket异步读取数据。接收来自服务器，其他客户端转发来的信息
                //AsyncCallback引用在异步操作完成时调用的回调方法
                //ClientSocket.BeginReceive(MsgBuffer, 0, MsgBuffer.Length, SocketFlags.None, new AsyncCallback(ReceiveCallBack), null);

                ClientSocket.Receive(MsgBuffer, SocketFlags.None);
                return Encoding.GetEncoding("utf-8").GetString(MsgBuffer);
            }
            catch (Exception ex)
            {
                throw;
                //MessageBox.Show("登录服务器失败，请确认服务器是否正常工作！" + ex.Message);
            }


        }

        private void ReceiveCallBack(IAsyncResult AR)
        {
            try
            {
                //结束挂起的异步读取，返回接收到的字节数。 AR，它存储此异步操作的状态信息以及所有用户定义数据
                int REnd = ClientSocket.EndReceive(AR);
                string s = Encoding.GetEncoding("utf-8").GetString(MsgBuffer, 0, REnd);
                //lock (this.RecieveMsg)
                //{
                //    this.RecieveMsg.AppendText(Encoding.GetEncoding("utf-8").GetString(MsgBuffer, 0, REnd));
                //}
                ClientSocket.BeginReceive(MsgBuffer, 0, MsgBuffer.Length, 0, new AsyncCallback(ReceiveCallBack), null);

            }
            catch
            {
                throw;
                //MessageBox.Show("已经与服务器断开连接！");
                // this.Close();
            }

        }

    }
}
